package org.gangcai.mac.shop.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.zhihu.matisse.Matisse;

import org.greenrobot.eventbus.EventBus;
import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.EventBusBaseBean;
import org.gangcai.mac.shop.bean.EventBusBaseBean1;
import org.gangcai.mac.shop.bean.PullAuthoBean;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.utils.MatisseUtils;
import org.gangcai.mac.shop.utils.UriUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class AuthoUserIdentifyImageActivity extends BaseAuthoActivity {

    @BindView(R.id.ic_zheng)
    ImageView icZheng;
    @BindView(R.id.ic_fan)
    ImageView icFan;
    @BindView(R.id.ic_shou)
    ImageView icShou;
    @BindView(R.id.nextStep)
    Button nextStep;
    private String userName;
    private String identifyNumber;
    private String addressDetail;
    private String city;
    /**
     * 身份证正面
     */
    private Uri icZhengUri;
    /**
     * 身份证反面
     */
    private Uri icFanUri;
    /**
     * 手持身份证
     */
    private Uri icShouUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        Bundle extras = getIntent().getExtras();
        userName = extras.getString("userName");
        identifyNumber = extras.getString("identifyNumber");
        city = extras.getString("city");
        addressDetail = extras.getString("addressDetail");
        getAuthoData("getrealname").subscribe(new CommonObserver<PullAuthoBean>() {
            @Override
            public void onSuccess(PullAuthoBean value) {
                loadImage(icZheng, value.getInfo().getImg_up());
                loadImage(icFan, value.getInfo().getImg_under());
                loadImage(icShou, value.getInfo().getImg_hand());
            }
        });
    }


    @Override
    protected String getTitleName() {
        return "实名认证";
    }

    @Override
    public int getcontentView() {
        return R.layout.activity_autho_user_identify_image;
    }

    @OnClick({R.id.ic_zheng, R.id.ic_fan, R.id.ic_shou, R.id.nextStep})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ic_zheng:

                MatisseUtils.pickImage(this, 1, 100);
                break;
            case R.id.ic_fan:
                MatisseUtils.pickImage(this, 1, 200);

                break;
            case R.id.ic_shou:
                MatisseUtils.pickImage(this, 1, 300);

                break;
            case R.id.nextStep:

                if (icZhengUri == null) {
                    ToastUtils.showShort("请上传身份证正面照片");
                } else if (icFanUri == null) {
                    ToastUtils.showShort("请上传身份证反面照片");
                } else if (icShouUri == null) {
                    ToastUtils.showShort("请上传手持身份证照片");
                } else {
                    submitData();
                }


                break;
        }
    }

    /**
     * 提交数据
     */
    private void submitData() {
        HashMap<String, File> fileMap = new HashMap<>();
        HashMap<String, String> paramMap = new HashMap<>();


        File fileUp = new File(UriUtils.getRealPathFromURI(this, icZhengUri));
        File fileDown = new File(UriUtils.getRealPathFromURI(this, icFanUri));
        File fileHand = new File(UriUtils.getRealPathFromURI(this, icShouUri));

        paramMap.put("uid", SPUtils.getInstance().getString("uid"));
        paramMap.put("real_name", userName);
        paramMap.put("id_number", identifyNumber);
        paramMap.put("city", city);
        paramMap.put("address", addressDetail);
        fileMap.put("img_up", fileUp);
        fileMap.put("img_under", fileDown);
        fileMap.put("img_hand", fileHand);

        prompDialog.showLoading("请等待");
        submitDataToServer("realname_ident", paramMap, fileMap).subscribe(new CommonObserver<CommonResonseBean>(prompDialog) {
            @Override
            public void onSuccess(CommonResonseBean value) {
                if (value.getCode() == Constant.CODE_SUCC) {
                    showMsg();
                    finish();
                    EventBus.getDefault().postSticky(new EventBusBaseBean1());
                    AuthoUserIdentifyActivity.close(getApplication());
                }
            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<Uri> uris;
        if (resultCode == RESULT_OK && data != null) {
            switch (requestCode) {
                case 100:
                    uris = Matisse.obtainResult(data);
                    if (uris != null && uris.size() > 0) {
                        icZhengUri = uris.get(0);
                        Glide.with(this).load(icZhengUri).into(icZheng);
                    }

                    break;
                case 200:
                    uris = Matisse.obtainResult(data);
                    if (uris != null && uris.size() > 0) {
                        icFanUri = uris.get(0);
                        Glide.with(this).load(icFanUri).into(icFan);
                    }
                    break;
                case 300:
                    uris = Matisse.obtainResult(data);
                    if (uris != null && uris.size() > 0) {
                        icShouUri = uris.get(0);
                        Glide.with(this).load(icShouUri).into(icShou);
                    }
                    break;
            }
        }

    }
}
